<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 菜单表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">

                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>

                <el-input  placeholder="名称搜" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>

                <el-button type="primary" @click="showAddDialog">添加菜单</el-button>
            </div>

            <!-- table中使用树结构必须有： :tree-props="{ children: 'menuList',hasChildren: true, }" -->
            <el-table
                :data="tableData"
                border
                class="table"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                row-key="id"
                default-expand-all
                :tree-props="{ children: 'menuList',hasChildren: true, }"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="菜单名称"></el-table-column>
                <el-table-column prop="component" label="关联组件"></el-table-column>
                <el-table-column prop="url" label="url"></el-table-column>
                <el-table-column prop="icon" label="图标"></el-table-column>
                <el-table-column label="状态">
                    <template #default="scope" >
                        <span :style="{color: scope.row.state?'green':'red'}">
                            {{ scope.row.state?'启用':'禁用' }}</span>
                            <el-switch
                                    v-model="scope.row.state"
                                    class="mb-2"
                                    :active-value="true"
                                    :inactive-value="false"
                                    @change="changeState(scope.row)"
                            />
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑菜单信息" v-model="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="关联组件">
                    <el-input v-model="form.component"></el-input>
                </el-form-item>
                <el-form-item label="url">
                    <el-input v-model="form.url"></el-input>
                </el-form-item>
                <el-form-item label="图标">
                    <el-input v-model="form.icon"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch
                            v-model="form.state"
                            class="mb-2"
                            active-text="启用"
                            inactive-text="禁用"
                            :active-value="true"
                            :inactive-value="false"
                    />
                </el-form-item>
                <el-form-item label="父菜单">
                    <el-select v-model="form.parentId" placeholder="请选择父菜单">
                        <el-option
                                v-for="item in tableData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                                :disabled="!item.state"
                        />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "Menu",
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            form: {
                name: '',
                component: '',
                url: '',
                icon: '',
                parentId: null,
                state: true,
            },
        };
    },
    mounted() {
        this.getData();
    },
    methods: {
        //修改状态
        changeState(row){

            //最终要修改状态的菜单集合
            const changeMenus = [];
            //放入当前菜单的id和状态
            changeMenus.push({id:row.id,state:row.state});

            //判断成立说明点击的是一级菜单
            if (row.parentId==null){

                this.$confirm("一级菜单如果禁用或启用，其子菜单也会一同禁用或启用，您确定操作吗？", "提示", {
                    type: "warning"
                }).then(()=>{

                    //点击确定   找出当前要修改的一级菜单的id和它包含的二级菜单的id
                    //遍历当前一级菜单的二级菜单
                    row.menuList.forEach(m=>{
                        changeMenus.push({id:m.id,state:row.state});
                    })

                    console.log(changeMenus);

                    this.$axios({
                        method: 'post',
                        url: '/menu/changeState',
                        data: changeMenus
                    }).then(res=>{
                        if(res.data.code==200){
                            //数据库修改成功了，但是页面中的二级菜单因为没有主动点击 状态还是以前的  需要重新查询数据 或者 更新显示状态
                            row.menuList.forEach(m=>{
                                m.state = row.state;
                            })
                        }
                    })

                }).catch(()=>{
                    //取消 状态修改回去
                    row.state = !row.state;
                })

            }else{

                //本身就是一个二级菜单  那么直接改就可以了

                this.$axios({
                    method: 'post',
                    url: '/menu/changeState',
                    data: changeMenus
                }).then(res=>{
                    if(res.data.code!=200){
                        row.state = !row.state;
                    }
                })
            }


        },
        //显示添加对话框
        showAddDialog(){
            this.form = {
                name: '',
                component: '',
                url: '',
                icon: '',
                parentId: null,
                state: true,
            }
            this.editVisible = true
        },
        // 从服务端获取分页数据
        getData() {
            this.$axios({
                method:'post',
                url:'/menu/tree',
                data: {}
            }).then(res=>{
                console.log(res);
                if(res.data.code == 200){
                    this.tableData = res.data.data;
                }
            })

        },
        // 触发搜索按钮
        handleSearch() {
            this.getData();
        },
        // 删除操作
        handleDelete(row) {
            // 二次确认删除
            this.$confirm("确定要删除吗？", "提示", {
                type: "warning"
            })
            .then(() => {
                //确定删除之后发送删除请求
                this.$axios({
                    method:'delete',
                    url:'/menu/'+row.id
                }).then(res=>{
                    if(res.data.code==200){
                        this.$message.success("删除成功");
                        this.getData();
                    }else{
                        this.$message.error("删除失败，请重试");
                    }
                })
            })
            .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            //将data中存储的选中id数组置为空
            this.multipleSelection = [];
            //遍历选中行数组 拿到id放入选中id的数组
            val.forEach(menu => {
                this.multipleSelection.push(menu.id);
            });
            console.log(this.multipleSelection)
        },
        //批量删除的方法
        delAllSelection() {

            //判断有没有选中项
            if(this.multipleSelection.length==0){
                this.$message.error("没有任何选中项，无法删除");
                return;
            }

            this.$confirm("确定要删除这几项吗？", "提示", {
                type: "warning"
            })
            .then(() => {

                this.$axios({
                    method:'post',
                    url:'/menu/batch',
                    data: this.multipleSelection
                }).then(res=>{
                    if(res.data.code==200){
                        this.$message.success("删除成功");
                        this.getData();
                    }else{
                        this.$message.error("删除失败，请重试");
                    }
                })
            })
            .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.form = Object.assign({},row);
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            //发送添加或修改请求
            this.$axios({
                method:'post',
                url: '/menu',
                data: this.form
            }).then(res=>{
                if(res.data.code == 200){
                    this.getData();
                    this.editVisible = false;
                    this.$message.success(`操作成功`);
                }else{
                    this.$message.error(`操作失败，请重试`);
                }
            })
           
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
